package cn.thr.pethome.user.service.impl;

import cn.thr.pethome.base.service.Impl.BaseServiceImpl;
import cn.thr.pethome.constants.BaseConstants;
import cn.thr.pethome.login.domain.PhoneRegister;
import cn.thr.pethome.user.domain.User;
import cn.thr.pethome.user.domain.UserVO;
import cn.thr.pethome.user.mapper.UserMapper;
import cn.thr.pethome.user.service.IUserService;
import cn.thr.pethome.util.AjaxResult;
import cn.thr.pethome.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thr
 * @since 2023-01-12
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User findByPhone(String Phone) {
        return userMapper.findByPhone(Phone);
    }

    @Override
    public void add(PhoneRegister phoneRegister) {
        User user = new User();
        user.setPassword(phoneRegister.getPassword());
        user.setCreatetime(new Date());
        user.setPhone(phoneRegister.getPhone());
        user.setUsername(phoneRegister.getPhone());
        user.setState(BaseConstants.User.STATE_NORMAL);
        userMapper.add(user);
    }

    @Override
    public AjaxResult phoneCodeLogin(UserVO uservo) {

        String phone = uservo.getPhone();
        String imageCode = uservo.getImageCode();
        String imageCodeKey = uservo.getImageCodeKey();

        if(phone == null || "".equals(phone.trim())){
            return AjaxResult.error().setMsg("手机号不能为空");
        }

        User user = userMapper.findByPhone(phone);

        if(user == null){
            return AjaxResult.error().setMsg("该账号未注册，请去注册");
        }
        //参数校验
        //判断图片验证码的key是否为空
        if(imageCodeKey == null || "".equals(imageCodeKey.trim())) {
            return AjaxResult.error().setMsg("非法访问！");
        }
        //判断图片验证码是否为空
        if(phone == null || "".equals(phone.trim())) {
            return AjaxResult.error().setMsg("手机号不能为空！");
        }
        //判断手机号是否为空
        if(imageCode == null || "".equals(imageCode.trim())) {
            return AjaxResult.error().setMsg("图片验证码不能为空！");
        }
        //前置条件
        //验证码校验
        //通过图片验证码的key去redis里面获取值
        Object value = redisTemplate.opsForValue().get(imageCodeKey);
        //判断图片验证码是否已经失效
        if(value == null){
            return AjaxResult.error().setMsg("图片验证码已失效，请重新获取！");
        }
        //判断验证码输入错误的情况
        if(!value.toString().equals(imageCode)){
            return AjaxResult.error().setMsg("图片验证码输入错误！");
        }
        //生成四位手机号验证码
        String phoneCode = StrUtils.getRandomString(4).toLowerCase();
        //手机号校验
        //设置短信验证码key的值

        //防止重发
        String keyTime = phone + BaseConstants.Vocode.REGISTER_TIME;
        //通过key把开始的时间从redis里面取出来
        Object o = redisTemplate.opsForValue().get(keyTime);
        if(o == null) {
            //添加业务键标识
            long startTime = System.currentTimeMillis();
            String key = phone + BaseConstants.Vocode.REGISTER;
            //写入redis
            redisTemplate.opsForValue().set(key,phoneCode,BaseConstants.Vocode.REGISTER_TIME_OUT, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(keyTime,startTime,BaseConstants.Vocode.REGISTER_TIME_OUT,TimeUnit.MINUTES);
        }else {
            //转成long类型
            Long st = Long.valueOf(o.toString());
            //获取到结束时间
            long endTime = System.currentTimeMillis();
            if(endTime - st < 60000) {
                return AjaxResult.error().setMsg("请不要重复获取验证码");
            }else {
                String key = phone + BaseConstants.Vocode.REGISTER;
                //重置时间
                phoneCode = (String) redisTemplate.opsForValue().get(key);
                long startTime = System.currentTimeMillis();
                //写入redis
                redisTemplate.opsForValue().set(key,phoneCode,BaseConstants.Vocode.REGISTER_TIME_OUT,TimeUnit.MINUTES);
                redisTemplate.opsForValue().set(keyTime,startTime,BaseConstants.Vocode.REGISTER_TIME_OUT, TimeUnit.MINUTES);
            }
        }

        //发送手机验证码
        //System.out.println(Message_Uid,Message_Key,phone,phoneCode);
        //SendPhoneMessageUtil.sendPhoneMessage(Message_Uid,Message_Key,phone,phoneCode);
        System.err.println(phoneCode);
        return AjaxResult.success().setMsg("短信验证码发送成功,请在"+BaseConstants.Vocode.REGISTER_TIME_OUT+"分钟内使用");
    }
}
